package com.zcx.qshop.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;
import com.zcx.helper.view.rebound.ReboundListView;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.CouponAdapter;
import com.zcx.qshop.conn.JsonCouponAsyGet;
import com.zcx.qshop.conn.JsonCoupondeleteAsyGet;
import com.zcx.qshop.dialog.WhetherDialog;
import com.zcx.qshop.view.TitleView;

/**
 * Created by Administrator on 2015/8/22 0022.
 */
public class CouponActivity extends QSActivity {

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.coupon_list_view) private ReboundListView reboundListView;

    private CouponAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_coupon);

        final String totalprice = getIntent().getStringExtra("totalprice");

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName("我的优惠券");

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        new JsonCouponAsyGet(QSApplication.QSPreferences.readUid(), new AsyCallBack<JsonCouponAsyGet.Info>(){

            @Override
            public void onSuccess(String toast, int type, JsonCouponAsyGet.Info info) throws Exception {

                reboundListView.setAdapter(adapter = new CouponAdapter(CouponActivity.this, reboundListView, info.coupons));

            }

        }).execute(this);

        reboundListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                JsonCouponAsyGet.Info.Coupon coupon = adapter.getItem(position);

                if (ConfirmOrderActivity.onOrderChangeListener != null) {

                    if (Double.valueOf(totalprice) >= Double.valueOf(coupon.fullprice)) {

                        ConfirmOrderActivity.onOrderChangeListener.onCouponChange(adapter.getItem(position));

                        finish();

                    } else {

                        UtilToast.show(CouponActivity.this, "该优惠券不符合使用要求");

                    }

                }

                if (OrderDetailActivity.onOrderChangeCallBack != null) {

                    if (Double.valueOf(totalprice) >= Double.valueOf(coupon.fullprice)) {

                        OrderDetailActivity.onOrderChangeCallBack.onCouponChange(adapter.getItem(position));

                        finish();

                    } else {

                        UtilToast.show(CouponActivity.this, "该优惠券不符合使用要求");

                    }

                }

            }

        });

        reboundListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

                new WhetherDialog(CouponActivity.this, "删除当前优惠券") {

                    @Override
                    protected void onYes() {

                        new JsonCoupondeleteAsyGet(QSApplication.QSPreferences.readUid(), adapter.getItem(position).cid, new AsyCallBack() {

                            @Override
                            public void onSuccess(String toast, int type, Object o) throws Exception {

                                adapter.remove(adapter.getItem(position));

                            }

                        }).execute(CouponActivity.this);

                    }

                }.show();

                return false;

            }

        });

    }

}
